
% This is the proposed policy situation.

function [i_best, Value] = Propolicy(Line, M, I, J, p, lambda, ci, cI, r)

% Set up original values.
S = zeros (I, J); Profit = zeros (1,M); i_best = zeros (1,M); Value = zeros(1,M);
 
% Wait for the nth call, find the best situation
for n = 1:M
    j = Line(n);  % The nth patient is of type j.
    % Then we should update S, for each situation, 
    % S is different, so we raised "temporary S"
    for i = 1:I
        tempS = S;
        tempS(i, j) = tempS(i, j) + 1;     % For each situation, there is a tempS.
        Nmax = max(sum(tempS'));      % Use transpose of matrix. (To get the upper limit.)
        % Then, find according Q and R.
        Q = findQ(tempS, I, p, Nmax);     % First colume infers k=0
        R = findR(Q, I, lambda, Nmax);      % Also like above.
        % Then, get the profit by function.
        Profit(i) = profit(Q, R, r, ci, cI, I);
    end

% Compare and get the best ones.
Value(n) = max(Profit);
i_best0 = find(Profit==Value(n));
i_best(n) = min(i_best0);  % Use the first max number.

% Then, we update S Matrix.
delta = zeros(I, J);         % Have to repete for every patient (n).
delta(i_best(n),j) = 1;     % Here, j is directly read, i_best is calculated.
S = S + delta;              % It have to be here.
Nmax = max(sum(S'));
end
end

% So we finish the basic function we will use.
